/*
* @Author: sc
* @Date:   2019-01-10 11:34:15
* @Last Modified by:   sc
* @Last Modified time: 2019-01-14 15:16:50
*/
import { observable, computed, action, configure } from 'mobx';

import Todo  from 'store/todo/index.jsx';

configure({
    enforceActions : "always"
});

class TodoList{
	@observable todos = [];
	@observable id    = 1;
	@observable total = 0;
	// 原子性，一次只能编辑一个
	@observable updateKey = true;
	// 搜索相关
	@observable searchKey = false;
	@observable searchList = [];

	// 分页相关，均为默认值
	@observable pageNum = 1;
	@observable nextPageNum = 0;
	@observable prevPageNum = 0;
	@observable pageSize = 5;
	totalPageNum = 1;
	// 获取待完成的列表
	@computed get unFinishedList(){
		// filter()方法过滤
		if(this.searchKey){
			return this.getPagedata(this.searchList);
		}else{
			return this.getPagedata(this.todos);
		}
	}

	// // 获取已完成的列表
	// @computed get finishedList(){
	// 	if(this.searchKey){
	// 		return this.searchList.slice(0).filter(todo => (todo.finished  && !todo.deleted));
	// 	}else{
	// 		return this.todos.slice(0).filter(todo => (todo.finished  && !todo.deleted));
	// 	}
	// }

	@computed get getPrevPageNum(){
		return this.pageNum > 1 ? this.pageNum - 1 : 1;
	}
	@computed get getNextPageNum(){
		return this.pageNum < this.totalPageNum ? this.pageNum + 1 : this.totalPageNum;
	}
	@computed get getCurrentPage(){
		if(this.pageNum > this.getTotalPages){
			return this.getTotalPages;
		}else{
			return this.pageNum;
		}
	}
	@computed get getTotalPages(){
		// filter()方法过滤
		if(this.searchKey){
			let result = this.searchList.slice(0).filter(todo =>
					 (!todo.deleted)).reverse();
			return this.getPages(result);
		}else{
			let result = this.todos.slice(0).filter(todo =>
					 (!todo.deleted)).reverse();
			return this.getPages(result);
		}
	}

	@computed get getTotal(){
		return  this.todos.slice(0).filter(todo => (!todo.deleted)).length;
	}
	@computed get getFinish(){
		return  this.todos.slice(0).filter(todo => (todo.finished && !todo.deleted)).length;
	}
	@computed get getLeft(){
		return  this.todos.slice(0).filter(todo => (!todo.finished && !todo.deleted)).length;
	}
	// 获取指定搜索内容，支持模糊查询
	@action setSearchList(searchInfo){
		let temp = this.todos.slice(0);
		this.searchList = [];
		let reg = new RegExp(searchInfo);//使用正则实现模糊查询
		temp.forEach(array =>{
			if(array.content.data.match(reg)){
				console.log('search-result:',array.content.data);
				this.searchList.push(array);
			}
		})
		console.log('searchList:',this.searchList);
	}

	// 添加
	@action addTodo = (content) => {
		if(!! content){
			this.todos.push(new Todo(this.id, content))
			this.id++;
		}
	}
	// 清除
	@action clear = () => {
		this.id = 1;
		this.todos = [];
		this.total = 0;
	}
	// 变更updateKey
	@action changeUpdateKey = () => {
		this.updateKey = !this.updateKey;
	}
	// searchKey
	@action setSearchKey = (flag) => {
		this.searchKey = flag;
	}
	// 上一页
	@action prev = () => {
		if(this.pageNum > 1){
			this.pageNum--;
		}
	}
	// 下一页
	@action next = () => {
		if(this.pageNum < this.totalPageNum){
			this.pageNum++;
		}
	}
	// 设置pagesize
	@action setPageSize = (size) => {
		this.pageSize = size;
	}
	// 计算分页，并返回页面数据
	getPagedata(todos){
		if(todos === undefined){
			todos = [];
		}
		let result = todos.slice(0).filter(todo =>
					 (!todo.deleted)).reverse();
		this.totalPageNum = this.getPages(result);
		let offset = this.getCurrentPage * this.pageSize;
		if(offset <= result.length){
			return result.slice((this.getCurrentPage-1) * this.pageSize, offset);
		}else{
			return result.slice((this.getCurrentPage-1) * this.pageSize);
		}
	}
	getPages(todos){
		if(todos.length == 0){
			return 1;
		}
		else if(todos.length % this.pageSize == 0){
			return parseInt(todos.length / this.pageSize);
		}else{
			return parseInt((todos.length / this.pageSize)) + 1;
		}
	}
}

export default new TodoList();